package chapter05

object Demo05_Lambda {
  def main(args: Array[String]): Unit = {
    val fun = (name: String) => { println(name)}
    fun("BFW")
    println("========================")
    //定义一个函数 以函数作为参数传入
    def f(func: String => Unit): Unit = {
      func("java")
    }
    f(fun)


    println("========================")
    //实际事例  定义一个“二元运算”函数，只操作1 和 2 两个数 ，但是具体运算通过参数传入
    def dualFunctionOneAndTwo(fun: (Int, Int) => Int): Int = {
      fun(1,2)
    }
    val add = (a: Int , b: Int) => a + b
    val minus = (a: Int , b: Int) => a - b
    println(dualFunctionOneAndTwo(add))
    println(dualFunctionOneAndTwo(minus))

    //匿名函数简化
    println(dualFunctionOneAndTwo((a: Int , b: Int) => a + b))
    println(dualFunctionOneAndTwo((a: Int , b: Int) => a - b))

    println(dualFunctionOneAndTwo((a , b) => a + b))
    println(dualFunctionOneAndTwo((a , b) => a - b))

    println(dualFunctionOneAndTwo(_ + _))
    println(dualFunctionOneAndTwo(_ - _))

    //如果是b - a的话就不可以是_ - _
    println(dualFunctionOneAndTwo((a , b) => b - a))
    println(dualFunctionOneAndTwo(-_ + _))

  }

}
